function [coef,f_hat]=ct(f,identifier,mode)
%[coef,f_hat]=ct(f,identifier,mode)
%Implementation of the curvelet transformation.
%for the standart partition of the fourier domain use identifier='mycandes'
%e.g. coef=ct(imread('lena256.pgm','mycandes');
%
%author: Sebastian Schmelcher version: 2012-06-20

%identiffier recognision
%identifier can be used to set the mode. Therefore identifier must be a
%cell with the following structure:
% {identifier,[bool_ct bool_ict],mode}
% where bool_ct determines of mode is used during the ct mehtod
%




mode_bool=false;
if(nargin>2)
    mode_bool=true;
end
if(iscell(identifier))
   if(length(identifier)>1)
       mode_bool=identifier{2}(1);
       if(mode_bool)
           mode=identifier{3};
       end
   end
   identifier=identifier{1};
end

printLog=1;%0=OFF,1=short info, 2=full info


[n,m]=size(f);


%Apply 2D FFT
f_hat=fftshift(fft2(f));


% fourier window if mode is given (see get_fourier_mask for usage)
if(mode_bool)
    f_hat=f_hat.*get_fourier_mask(mode,[n,m]);
end


%Get the index structure for the given identifier
%length of indexStruct=number radial indicies
%indexStruct(i)=number angular indicies for the i-th radial index
indexStruct=get_indexStruct(identifier,[n,m]);
num_radial=length(indexStruct);


%Preallocation of the coeffcient cell-matrix
coef=cell(num_radial+1,1);



%Low-pass:
curvelet=get_curvelet(identifier,[n,m],0,0);
low=ifft2(ifftshift(curvelet));

if(printLog==1)
    fprintf(['ct (' num2str(num_radial) '): ']);
end

for radial=1:num_radial %radial scale
        if(printLog>0)
            if(printLog>1)
                fprintf(['ct: Iteration i=' num2str(radial) ' of ' num2str(num_radial) ' \n']);
            else
                fprintf('.');
                if(radial==num_radial)
                    fprintf('\n');
                end
            end
        end
    num_angular=indexStruct(radial);
    %preallocation
    coef{radial}=cell(num_angular,1);
    for angular=1:num_angular %angular scale
        %fprintf(['Iteration j=' num2str(angular) ' of ' num2str(indexStruct(radial)) ' \n']);
        if(printLog>1)
            fprintf('.');
            if(angular==num_angular)
                fprintf('\n');
            end
        end

            
        
        %compute curvelet in fourier domain for this radial and angular index
        curvelet=get_curvelet(identifier,[n,m],radial,angular);

        %Multiply with window/curvelet and IFFt
        coef{radial}{angular}=ifft2(ifftshift(f_hat.*curvelet));

        
    end
end


%Low-pass is stored at the end
coef{num_radial+1}=low;


